//
//  AlbumPickerController.m
//  YCSchool
//
//  Created by yanglinxia on 16/5/3.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  相册选择器视图控制器

#import "AlbumPickerController.h"
#import "AlbumPickerCell.h"
#import "YCAssetsManager.h"


@interface AlbumPickerController ()<UITableViewDataSource,UITableViewDelegate>

///表格视图
@property (nonatomic, strong) UITableView *tableView;
///相册分组
@property (nonatomic, strong) NSArray *assetsGroups;

///最多可以选择的数量
@property (nonatomic, assign) NSUInteger maxNumberOfImages;
///完成回调
@property (nonatomic, copy)  MultipleImagePickerControllerCompletionBlock completion;
///取消回调
@property (nonatomic, copy)  MultipleImagePickerControllerCancelBlock cancel;
@end

@implementation AlbumPickerController

#pragma mark - 属性获取方法

- (UITableView *)tableView
{
    if(!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _tableView.rowHeight = 60;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.contentInset = UIEdgeInsetsZero;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

#pragma mark - 生命周期

- (instancetype)initWithmaxNumberOfImages:(NSUInteger)maxNumberOfImages
                               completion:(MultipleImagePickerControllerCompletionBlock)completion
                                   cancel:(MultipleImagePickerControllerCancelBlock)cancel
{
    if(self = [super init])
    {
        _maxNumberOfImages = maxNumberOfImages;
        _completion = completion;
        _cancel = cancel;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"相册", nil);
    //
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button sizeToFit];
    [button addTarget:self action:@selector(didTouchUpInsideCancelBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
    [self.view addSubview:self.tableView];
    
    _assetsGroups = [YCAssetsManager shareManager].assetsGroups;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - AlbumPickerController 自定义方法

- (void)didTouchUpInsideCancelBarButtonItem:(UIBarButtonItem *)rightItem
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.assetsGroups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *IDEN = @"CELLID";
    AlbumPickerCell *cell = [tableView dequeueReusableCellWithIdentifier:IDEN];
    ALAssetsGroup *group = self.assetsGroups[indexPath.row];
    if(!cell)
    {
        cell = [[AlbumPickerCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:IDEN];
        cell.textLabel.numberOfLines = 1;
        cell.selectionStyle = UITableViewCellSelectionStyleNone ;
    }
    CGImageRef posterImage = group.posterImage;
    size_t height = CGImageGetHeight(posterImage);
    float scale = height / 78.0f;
    cell.imageView.image = [UIImage imageWithCGImage:posterImage scale:scale orientation:UIImageOrientationUp];
    
    //每组照片数量
    NSString *numText = [NSString stringWithFormat:@"(%ld)",(unsigned long)group.numberOfAssets];
    //组名
    NSString *groupName = indexPath.row == 0 ? @"所有照片" : [group valueForProperty:ALAssetsGroupPropertyName];
    NSString *text = [NSString stringWithFormat:@"  %@   %@",groupName,numText];
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text];
    [attrString addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:129/255.0 green:129/255.0 blue:129/255.0 alpha:1]} range:[text rangeOfString:numText]];
    
    cell.textLabel.attributedText = attrString;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALAssetsGroup *group = self.assetsGroups[indexPath.row];
    MultipleImagePickerViewController *vc = [[MultipleImagePickerViewController alloc] initWithAssetGroup:group maxNumberOfImages:_maxNumberOfImages completion:_completion cancel:_cancel];
    NSString *groupName = indexPath.row == 0 ? @"所有照片" : [group valueForProperty:ALAssetsGroupPropertyName];
    vc.title = groupName;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
